﻿Imports System.Data
Imports System.Data.OleDb
Public Class cakeProfile
    Inherits System.Web.UI.Page
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'see if the user has favourited this cake
        Dim oleDbConn13 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        Dim SqlString13 As String = "SELECT * FROM faves WHERE caker = @caker AND cake_fkid = @ID"
        Dim cmd13 As OleDbCommand = New OleDbCommand(SqlString13, oleDbConn13)
        oleDbConn13.Open()
        cmd13.CommandType = CommandType.Text
        cmd13.Parameters.AddWithValue("@caker", User.Identity.Name)
        cmd13.Parameters.AddWithValue("@cake_fkid", Request.Params("ID"))
        Dim dataReader13 = cmd13.ExecuteReader()
        dataReader13.Read()

        'and make the heart pink if they have
        If dataReader13.HasRows Then
            Dim faveRead As Boolean = dataReader13.GetValue(4)
            If faveRead = True Then
                cb_fave.Checked = True
            End If
        End If
        Dim SqlString0 As String = "SELECT * FROM numCakeFaves WHERE cake_fkid=@f1"
        Dim cmd0 As OleDbCommand = New OleDbCommand(SqlString0, oleDbConn13)
        cmd0.CommandType = CommandType.Text
        cmd0.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dataReader0 = cmd0.ExecuteReader()
        lv_numFaves.DataSource = dataReader0
        lv_numFaves.DataBind()
        dataReader0.Close()

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Request.Params("ID") = "" Then Response.Redirect("list.aspx")

        Dim currentLoc = Request.Url.AbsoluteUri
        If currentLoc.Contains("&replyTo") And Not currentLoc.Contains("&repliedto") Then
            pnl_replyComment.Visible = True
        End If

        If IsPostBack Then
            Dim currentLoc2 = Request.Url.AbsoluteUri
            If currentLoc2.Contains("&repliedto") Then
                pnl_replyComment.Visible = False
            End If
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        Dim cmdTag As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM cake WHERE ID=@f1", oleDbConn)
        cmdTag.SelectCommand.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dsTag As New DataSet()
        cmdTag.Fill(dsTag, "cake")
        Dim cmdTaga As OleDbDataAdapter = New OleDbDataAdapter("SELECT DISTINCT * FROM cakeTagView WHERE cake_fkid=@f1", oleDbConn)
        cmdTaga.SelectCommand.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmdTaga.Fill(dsTag, "cakeTagView")
        dsTag.Relations.Add("cakeTagRel", _
        dsTag.Tables("cake").Columns("ID"), _
        dsTag.Tables("cakeTagView").Columns("cake_fkid"))
        ParentLv.DataSource = dsTag.Tables(0)
        ParentLv.DataBind()

        Dim cmd4 As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM cakeComments WHERE cake_fkid=@f1 ORDER BY ID DESC", oleDbConn)
        cmd4.SelectCommand.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim ds4 As New DataSet()
        cmd4.Fill(ds4, "cakeComments")
        Dim cmd4a As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM replies WHERE cake_fkid=@f1 AND reply NOT LIKE '0'", oleDbConn)
        cmd4a.SelectCommand.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd4a.Fill(ds4, "replies")
        ds4.Relations.Add("myrelation", _
        ds4.Tables("cakeComments").Columns("ID"), _
        ds4.Tables("replies").Columns("replyTo"))
        parentRepeater.DataSource = ds4.Tables(0)
        parentRepeater.DataBind()



        Dim oleDbConn1 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        Dim SqlString1 As String = "SELECT * FROM cake WHERE ID=@f1"
        Dim cmd1 As OleDbCommand = New OleDbCommand(SqlString1, oleDbConn1)
        cmd1.CommandType = CommandType.Text
        cmd1.Parameters.AddWithValue("@f1", Request.Params("ID"))
        oleDbConn1.Open()
        Dim dataReader = cmd1.ExecuteReader()
        lv_mainCake.DataSource = dataReader
        lv_mainCake.DataBind()
        dataReader.Close()
        Dim dataReader2 = cmd1.ExecuteReader()
        lv_title.DataSource = dataReader2
        lv_title.DataBind()
        dataReader2.Close()


        Dim connectionString As String
        Dim oledbCnn As OleDbConnection
        Dim oledbCmd As OleDbCommand

        Dim sql As String

        connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\diycake.accdb;Persist Security Info=True"
        sql = "SELECT cakeTitle FROM cake WHERE ID = @f1"

        oledbCnn = New OleDbConnection(connectionString)

        Try
            oledbCnn.Open()
            oledbCmd = New OleDbCommand(sql, oledbCnn)
            oledbCmd.CommandType = CommandType.Text
            oledbCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
            While oledbReader.Read
                Page.Title = "My DIY Wedding Cake - " & oledbReader.Item(0)
            End While

            oledbReader.Close()
            oledbCmd.Dispose()
            oledbCnn.Close()
        Catch ex As Exception
            Response.Write("Can not open connection!")
        End Try

        If IsPostBack = False Then
            Dim oleDbConn2 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
            Dim SqlString22 As String = "INSERT INTO cakeViews (caker, cake_fkid) VALUES (@f1,@f2)"
            Dim cmd22 As OleDbCommand = New OleDbCommand(SqlString22, oleDbConn2)
            cmd22.CommandType = CommandType.Text
            cmd22.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmd22.Parameters.AddWithValue("@f2", Request.Params("ID"))
            oleDbConn2.Open()
            cmd22.ExecuteNonQuery()
        End If

    End Sub

    Protected Function getAge(ByVal age As Integer)
        Dim retval As String
        retval = ""
        Select Case age
            Case Is < 20
                Return retval + "<img src='img/upto19.png'/>"
            Case Is < 30
                Return retval + "<img src='img/upto29.png'/>"
            Case Is < 40
                Return retval + "<img src='img/upto39.png'/>"
            Case Is < 50
                Return retval + "<img src='img/upto49.png'/>"
            Case Is < 60
                Return retval + "<img src='img/upto59.png'/>"
            Case Is > 59
                Return retval + "<img src='img/60plus.png'/>"
        End Select
        Return retval
    End Function


    Protected Function getStars(ByVal cakeStar As Integer)
        Dim retval As String
        retval = ""
        For i = 1 To cakeStar
            retval = retval + "<img src='img/cupcake.png' />"
        Next
        Return retval
    End Function
    Protected Function getUserStars(ByVal userStar As Integer)
        Dim retval As String
        retval = ""
        For i = 1 To userStar
            retval = retval + "<img src='img/star2.png' />"
        Next
        Return retval
    End Function
    Protected Function getUserAvgRating(ByVal userAvgStar As Integer)

        Dim retval As String
        retval = ""
        For i = 1 To userAvgStar
            retval = retval + "<img src='img/star2.png' />"
        Next
        Return retval
        

    End Function
    Protected Function getDifficulty(ByVal difficulty As Integer)
        Dim retval As String
        retval = ""
        For i = 1 To difficulty
            retval = retval + "<img src='img/difficulty.png' />"
        Next
        Return retval
    End Function
    Protected Sub Unnamed1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)

    End Sub
    Protected Sub lv_mainCake_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lv_mainCake.SelectedIndexChanged

    End Sub
    Protected Sub lv_title_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lv_title.SelectedIndexChanged

    End Sub
    Protected Sub btn_addComment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_addComment.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into cakeComments(commentAuthor,comment,cake_fkid,commentDateAdded, replyTo) Values (@commentAuthor,@comment,@cake_fkid,@commentDateAdded, @replyTo)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@commentAuthor", User.Identity.Name)
        cmd.Parameters.AddWithValue("@comment", tb_addComment.Text)
        cmd.Parameters.AddWithValue("@cake_fkid", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@commentDateAdded", DateTime.Now.ToString())
        cmd.Parameters.AddWithValue("@replyTo", 0)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        tb_addComment.Text = ""


        'Show the ID of the last comment added
        Dim getLastComment As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM cakeComments ORDER BY ID DESC", oleDbConn)
        Dim dr4 = getLastComment.ExecuteReader()
        dr4.Read()
        Dim lastIdComment = dr4.GetValue(0)
        dr4.Close()

        Dim SqlString2 As String = "Insert into replies(reply, replyDateAdded, replyTo, cake_fkid, replyAuthor) Values (@reply, @replyDateAdded, @replyTo, @cake_fkid, @replyAuthor)"
        Dim cmd2 As OleDbCommand = New OleDbCommand(SqlString2, oleDbConn)
        cmd2.CommandType = CommandType.Text
        cmd2.Parameters.AddWithValue("@reply", "0")
        cmd2.Parameters.AddWithValue("@replyDateAdded", DateTime.Now.ToString())
        cmd2.Parameters.AddWithValue("@replyTo", lastIdComment)
        cmd2.Parameters.AddWithValue("@cake_fkid", Request.Params("ID"))
        cmd2.Parameters.AddWithValue("@replyAuthor", User.Identity.Name)
        cmd2.ExecuteNonQuery()

        'pic
        Dim oleDbConn1 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        Dim SqlString1 As String = "SELECT userImg FROM cakers WHERE userUName=@f1"
        Dim cmd1 As OleDbCommand = New OleDbCommand(SqlString1, oleDbConn)
        cmd1.CommandType = CommandType.Text
        cmd1.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbConn1.Open()
        Dim dataReader = cmd1.ExecuteReader()
        dataReader.Read()
        Dim pic = dataReader.GetValue(0)
        dataReader.Close()

        Dim SqlString3 As String = "UPDATE cakeComments SET userImg=@f1 WHERE commentAuthor=@f2"
        Dim cmd3 As OleDbCommand = New OleDbCommand(SqlString3, oleDbConn)
        cmd3.CommandType = CommandType.Text
        cmd3.Parameters.AddWithValue("@f1", pic)
        cmd3.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd3.ExecuteNonQuery()

        Dim oleDbConn4 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        Dim cmd4 As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM cakeComments WHERE cake_fkid=@f1 ORDER BY ID DESC", oleDbConn)
        cmd4.SelectCommand.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim ds4 As New DataSet()
        cmd4.Fill(ds4, "cakeComments")
        Dim cmd4a As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM replies WHERE cake_fkid=@f1 AND reply NOT LIKE '0'", oleDbConn)
        cmd4a.SelectCommand.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd4a.Fill(ds4, "replies")
        ds4.Relations.Add("myrelation", _
        ds4.Tables("cakeComments").Columns("ID"), _
        ds4.Tables("replies").Columns("replyTo"))
        parentRepeater.DataSource = ds4.Tables(0)
        parentRepeater.DataBind()
        oleDbConn4.Close()
    End Sub

    Protected Sub btn_replyComment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_replyComment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into replies (replyAuthor,reply,replyDateAdded, replyTo,cake_fkid) Values (@replyAuthor,@reply,@replyDateAdded, @replyTo, @cake_fkid)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@replyAuthor", User.Identity.Name)
        cmd.Parameters.AddWithValue("@reply", tb_replyComment.Text)
        cmd.Parameters.AddWithValue("@replyDateAdded", DateTime.Now.ToString())
        cmd.Parameters.AddWithValue("@replyTo", Request.QueryString("replyTo"))
        cmd.Parameters.AddWithValue("@cake_fkid", Request.Params("ID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        tb_replyComment.Text = ""

        'pic
        Dim SqlString1 As String = "SELECT userImg FROM cakers WHERE userUName=@f1"
        Dim cmd1 As OleDbCommand = New OleDbCommand(SqlString1, oleDbConn)
        cmd1.CommandType = CommandType.Text
        cmd1.Parameters.AddWithValue("@f1", User.Identity.Name)
        Dim dataReader = cmd1.ExecuteReader()
        dataReader.Read()
        Dim pic1 = dataReader.GetValue(0)

        Dim SqlString8 As String = "UPDATE replies SET userImg = @f1 WHERE replyAuthor=@f2"
        Dim cmd8 As OleDbCommand = New OleDbCommand(SqlString8, oleDbConn)
        cmd8.CommandType = CommandType.Text
        cmd8.Parameters.AddWithValue("@f1", pic1)
        cmd8.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd8.ExecuteNonQuery()

        Dim cmd4 As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM cakeComments WHERE cake_fkid=@f1 ORDER BY ID DESC", oleDbConn)
        cmd4.SelectCommand.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim ds4 As New DataSet()
        cmd4.Fill(ds4, "cakeComments")
        Dim cmd4a As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM replies WHERE cake_fkid=@f1 AND reply NOT LIKE '0'", oleDbConn)
        cmd4a.SelectCommand.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd4a.Fill(ds4, "replies")
        ds4.Relations.Add("myrelation", _
        ds4.Tables("cakeComments").Columns("ID"), _
        ds4.Tables("replies").Columns("replyTo"))
        parentRepeater.DataSource = ds4.Tables(0)
        parentRepeater.DataBind()

        Dim getLastReply As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM replies ORDER BY ID DESC", oleDbConn)
        Dim dr5 = getLastReply.ExecuteReader()
        dr5.Read()
        Dim lastIdReply = dr5.GetValue(0)
        Dim currentLoc = Request.Url.AbsoluteUri
        Dim replyLoc = currentLoc & "&repliedto" & "#" & lastIdReply
        Response.Redirect(replyLoc)



    End Sub
    Protected Sub goPdf2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles goPdf2.Click
        Dim oleDbConn1 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        Dim SqlString1 As String = "SELECT cakeTitle FROM cake WHERE ID=@f1"
        Dim cmd1 As OleDbCommand = New OleDbCommand(SqlString1, oleDbConn1)
        cmd1.CommandType = CommandType.Text
        cmd1.Parameters.AddWithValue("@f1", Request.Params("ID"))
        oleDbConn1.Open()
        Dim dataReader = cmd1.ExecuteReader()
        dataReader.Read()
        Dim cakeTitle = dataReader.GetValue(0)
        Dim now As DateTime = DateTime.Now
        Dim pdfName As String = String.Format("My_DIY_Wedding_Cake_" & cakeTitle & "_" & "{0:dd-MM-yyyy}", now)
        ASPXToPDF1.RenderAsPDF(pdfName)
        Dim SqlString11 As String = "INSERT INTO pdfDownloads (cake_fkid, downloadDateAdded ) Values (@cake_fkid,@downloadDateAdded )"
        Dim cmd11 As OleDbCommand = New OleDbCommand(SqlString11, oleDbConn1)
        cmd11.CommandType = CommandType.Text
        cmd11.Parameters.AddWithValue("@cake_fkid", Request.Params("ID"))
        cmd11.Parameters.AddWithValue("@downloadDateAdded", Date.Now.ToString())
        cmd11.ExecuteNonQuery()
    End Sub


    Protected Sub ds_relCakes_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles ds_relCakes.Selecting

    End Sub

    Protected Sub cb_fave_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cb_fave.CheckedChanged

        Dim oleDbConn13 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        Dim SqlString13 As String = "SELECT * FROM faves WHERE caker = @caker AND cake_fkid = @ID"
        Dim cmd13 As OleDbCommand = New OleDbCommand(SqlString13, oleDbConn13)
        oleDbConn13.Open()
        cmd13.CommandType = CommandType.Text
        cmd13.Parameters.AddWithValue("@caker", User.Identity.Name)
        cmd13.Parameters.AddWithValue("@cake_fkid", Request.Params("ID"))
        Dim dataReader13 = cmd13.ExecuteReader()
        dataReader13.Read()

        If dataReader13.HasRows Then
            Dim faveRead As Boolean = dataReader13.GetValue(4)

            'used to be a fave
            If faveRead = False Then
                Dim SqlString14 As String = "UPDATE faves SET faved=@faved, faveDateAdded=@faveDateAdded WHERE caker = @caker AND cake_fkid = @cake_fkid"
                Dim cmd14 As OleDbCommand = New OleDbCommand(SqlString14, oleDbConn13)
                cmd14.CommandType = CommandType.Text
                cmd14.Parameters.AddWithValue("@faved", True)
                cmd14.Parameters.AddWithValue("@faveDateAdded", DateTime.Now.ToString())
                cmd14.Parameters.AddWithValue("@caker", User.Identity.Name)
                cmd14.Parameters.AddWithValue("@cake_fkid", Request.Params("ID"))
                cmd14.ExecuteNonQuery()
                cb_fave.Checked = True
            End If

            'is a fave
            If faveRead = True Then
                Dim SqlString14 As String = "UPDATE faves SET faved=@faved, faveDateAdded=@faveDateAdded WHERE caker = @caker AND cake_fkid = @cake_fkid"
                Dim cmd14 As OleDbCommand = New OleDbCommand(SqlString14, oleDbConn13)
                cmd14.CommandType = CommandType.Text
                cmd14.Parameters.AddWithValue("@faved", False)
                cmd14.Parameters.AddWithValue("@faveDateAdded", DateTime.Now.ToString())
                cmd14.Parameters.AddWithValue("@caker", User.Identity.Name)
                cmd14.Parameters.AddWithValue("@cake_fkid", Request.Params("ID"))
                cmd14.ExecuteNonQuery()
                cb_fave.Checked = False
            End If


        End If

        'was never a fave
        If Not dataReader13.HasRows Then
            dataReader13.Close()
            Dim SqlString11 As String = "INSERT INTO faves (cake_fkid, caker, faveDateAdded, faved ) Values (@cake_fkid,@caker_fkid,@faveDateAdded, @faved)"
            Dim cmd11 As OleDbCommand = New OleDbCommand(SqlString11, oleDbConn13)
            cmd11.CommandType = CommandType.Text
            cmd11.Parameters.AddWithValue("@cake_fkid", Request.Params("ID"))
            cmd11.Parameters.AddWithValue("@caker", User.Identity.Name)
            cmd11.Parameters.AddWithValue("@faveDateAdded", DateTime.Now.ToString())
            cmd11.Parameters.AddWithValue("@faved", True)
            cmd11.ExecuteNonQuery()
            cb_fave.Checked = True

        End If

        Dim SqlString0 As String = "SELECT * FROM numCakeFaves WHERE cake_fkid=@f1"
        Dim cmd0 As OleDbCommand = New OleDbCommand(SqlString0, oleDbConn13)
        cmd0.CommandType = CommandType.Text
        cmd0.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dataReader0 = cmd0.ExecuteReader()
        lv_numFaves.DataSource = dataReader0
        lv_numFaves.DataBind()
        dataReader0.Close()
        oleDbConn13.Close()
    End Sub
    Protected Sub btn_rate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_rate.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim addBakersStarSql As String = "INSERT INTO cakersRating (author, rating) VALUES (@f1, @f2)"
        Dim addBakersStarCmd As OleDbCommand = New OleDbCommand(addBakersStarSql, oleDbConn)
        addBakersStarCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        addBakersStarCmd.Parameters.AddWithValue("@f2", Rating1.CurrentRating)
        addBakersStarCmd.ExecuteNonQuery()
        'Close the command objects to help the next page update
        addBakersStarCmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        'Redirect the user to the page with the updated rating
        Response.Redirect("cakeProfile.aspx?ID=" & Request.Params("ID"))
    End Sub
    Protected Sub btn_addTag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_addTag.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("diycakeConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim newTags = tb_tag.Text.Split(",")
        For Each newTag In newTags
            Dim newTagSql As String = "INSERT INTO tag (tag) VALUES (@cakeTag)"
            Dim cmd2 As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd2.CommandType = CommandType.Text
            cmd2.Parameters.AddWithValue("@cakeTag", newTag)
            cmd2.ExecuteNonQuery()
            Dim getLastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM tag ORDER BY ID DESC", oleDbConn)
            Dim dr = getLastIdCmd.ExecuteReader()
            dr.Read()
            Dim lastIdTag = dr.GetValue(0)

            'Show the ID of the last cake added
            Dim getLastIdCakeCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM cake WHERE ID=@f1", oleDbConn)
            getLastIdCakeCmd.CommandType = CommandType.Text
            getLastIdCakeCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            Dim dr2 = getLastIdCakeCmd.ExecuteReader()
            dr2.Read()
            Dim lastIdCake = dr2.GetValue(0)

            'Associate cake with tag
            Dim SqlString2 As String = "Insert into cakeTagJunction(cake_fkid, tag_fkid) Values (@f1,@f2)"
            Dim newcmd As OleDbCommand = New OleDbCommand(SqlString2, oleDbConn)
            newcmd.CommandType = CommandType.Text
            newcmd.Parameters.AddWithValue("@f1", lastIdCake)
            newcmd.Parameters.AddWithValue("@f2", lastIdTag)
            newcmd.ExecuteNonQuery()
        Next
        tb_tag.Text = ""
        Dim cmdTag As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM cake WHERE ID=@f1", oleDbConn)
        cmdTag.SelectCommand.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dsTag As New DataSet()
        cmdTag.Fill(dsTag, "cake")
        Dim cmdTaga As OleDbDataAdapter = New OleDbDataAdapter("SELECT DISTINCT * FROM cakeTagView WHERE cake_fkid=@f1", oleDbConn)
        cmdTaga.SelectCommand.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmdTaga.Fill(dsTag, "cakeTagView")
        dsTag.Relations.Add("cakeTagRel", _
        dsTag.Tables("cake").Columns("ID"), _
        dsTag.Tables("cakeTagView").Columns("cake_fkid"))
        ParentLv.DataSource = dsTag.Tables(0)
        ParentLv.DataBind()
    End Sub
End Class
